package com.dcrain.api.authz.common;

import java.util.HashMap;

/**
 * 返回数据
 * 
 */
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;
	
	public R() {
		put("apiCode", ApiCodeEnum.SUCCESS.getCode());
		put("success",true);
		put("apiMessage", ApiCodeEnum.SUCCESS.getMsg());
	}
	
	public static R ok() {
		return new R();
	}
	
	public static R ok(Object obj) {
		R r = new R();
		r.put("data", obj);
		return r;
	}
	
	public static R error() {
		R r = new R();
		r.put("success",false);
		r.put("apiCode", ApiCodeEnum.UNKNOW_ERROR.getCode());
		r.put("apiMessage", ApiCodeEnum.UNKNOW_ERROR.getMsg());
		return r;
	}
	
	public static R error(Object obj) {
		R r = error();
		r.put("data",obj);
		return r;
	}
	
	public static R error(String msg) {
		R r = error();
		r.put("apiMessage",msg);
		return r;
	}
	
	public static R error(ApiCodeEnum code) {
		R r = error();
		r.put("apiCode", code.getCode());
		r.put("apiMessage", code.getMsg());
		return r;
	}
	
	public static R error(ApiCodeEnum code,String msg) {
		R r = error();
		r.put("apiCode", code.getCode());
		r.put("apiMessage", code.getMsg()+":"+msg);
		return r;
	}
	
	public static R error(ApiCodeEnum code,Object obj) {
		R r = error(code);
		r.put("data", obj);
		return r;
	}
	
	public static R error(String apiCode, String apiMessage) {
		R r = error();
		r.put("apiCode", apiCode);
		r.put("apiMessage", apiMessage);
		return r;
	}

	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}
